using System.IO;
using Bingo.Helper;
using QFramework;
using UnityEditor;
using UnityEditor.Build.Content;
using UnityEngine;

namespace BingoEditor
{
    public static class BuildHelper
    {
        private const string relativeDirPrefix = "../Release";

        public static string BuildFolder = "../Release/{0}/StreamingAssets/";


        public static void Build(PlatformType type, BuildAssetBundleOptions buildAssetBundleOptions,
            BuildOptions buildOptions, bool isBuildExe, bool isContainAB, bool isBinary)
        {
            BuildTarget buildTarget = BuildTarget.StandaloneWindows;
            string exeName = "HiBall";
            BuildTargetGroup group = BuildTargetGroup.Android;
            switch (type)
            {
                case PlatformType.PC:
                    buildTarget = BuildTarget.StandaloneWindows64;
                    group = BuildTargetGroup.Standalone;
                    exeName += ".exe";
                    break;
                case PlatformType.Android:
                    buildTarget = BuildTarget.Android;
                    group = BuildTargetGroup.Android;
                    exeName += ".apk";
                    break;
                case PlatformType.IOS:
                    group = BuildTargetGroup.iOS;
                    buildTarget = BuildTarget.iOS;
                    break;
                case PlatformType.MacOS:
                    group = BuildTargetGroup.Standalone;
                    buildTarget = BuildTarget.StandaloneOSX;
                    break;
            }

            PlayerSettings.SetScriptingBackend(group, isBinary ? ScriptingImplementation.IL2CPP : ScriptingImplementation.Mono2x);

            string fold = string.Format(BuildFolder, type);
            if (!Directory.Exists(fold))
            {
                Directory.CreateDirectory(fold);
            }

            BuildScript.BuildAssetBundles(buildTarget);
//			BuildPipeline.BuildAssetBundles(fold, buildAssetBundleOptions, buildTarget);
//			GenerateVersionInfo(fold);

            if (isContainAB)
            {
                FileHelper.CleanDirectory("Assets/StreamingAssets/");
                FileHelper.CopyDirectory(fold,"Assets/StreamingAssets/" + ResKitUtil.GetPlatformForAssetBundles(buildTarget));
            }

            if (isBuildExe)
            {
                AssetDatabase.Refresh();
                string[] levels = {"Assets/Scenes/GameEntry.unity"};

                BuildPipeline.BuildPlayer(levels, $"{relativeDirPrefix}/{exeName}", buildTarget, buildOptions);
            }
        }
    }
}